//
//  GMTodayTaskVC.m
//  Teecloud
//
//  Created by 小熊 on 2018/7/4.
//  Copyright © 2018年 小熊. All rights reserved.
//

#import "GMTodayTaskVC.h"

//Controller
//Model
#import "XZTaskModel.h"
//View
#import "GMTodayTaskCell.h"
@interface GMTodayTaskVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tabView;
@property (nonatomic, assign) BOOL isFirst;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end
static NSString *identifier = @"TodayTaskCell";
@implementation GMTodayTaskVC
#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"今日任务";
    self.view.backgroundColor = kWhiteColor;
    [self getData];
}
#pragma mark - Networking
- (void)getData{
    NSDictionary *dict= @{
                          @"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId]
                          };
    kWeakObject(self)
    if (!self.isFirst) {
        self.isFirst = YES;
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"/ajax-api.ashx?$c=TeeyunApp&$m=GetTodayTask&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                [weakself.dataSource removeAllObjects];
                NSArray *array= [XZTaskModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:RESULT_NAME_Data]];
                [weakself.dataSource addObjectsFromArray:array];
                [weakself.tabView.mj_header endRefreshing];
                if (weakself.dataSource.count == 0) {
                    [weakself.tabView setupEmptyDataText:@"暂无数据" secoundText:@"" buttonTit:@"" verticalOffset:0 emptyImage:NODATAIMG tapBlock:^{
                        
                    } butClick:^{
                        
                    }];
                }
                [weakself.tabView reloadData];
            }else{
                [weakself.tabView.mj_header endRefreshing];
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
            
        }else{
            [weakself.tabView.mj_header endRefreshing];
            [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakself.view];
        }
    }];
}
#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GMTodayTaskCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    XZTaskModel *model = self.dataSource[indexPath.row];
    cell.model = model;
    return cell;
}

#pragma mark - setter
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (UITableView *)tabView{
    if (!_tabView) {
        _tabView =[UITableView zj_tableViewWithSuperview:self.view delegate:self style:UITableViewStylePlain constraints:^(MASConstraintMaker *make) {
            make.top.leading.trailing.equalTo(self.view);
            if (CYL_IS_IPHONE_X) {
                make.bottom.equalTo(self.view).offset(-34);
            }else{
                make.bottom.equalTo(self.view);
            }
        }];
        _tabView.rowHeight = 60.f;
        //注册cell
        [_tabView registerNib:[UINib nibWithNibName:NSStringFromClass([GMTodayTaskCell class]) bundle:nil] forCellReuseIdentifier:identifier];
        _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tabView.showsHorizontalScrollIndicator = false;
        _tabView.showsVerticalScrollIndicator =false;
        _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    }
    return _tabView;
}
@end
